package com.bobi.tfca.ui.mainnavigation.message.chatroom

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.lifecycle.ViewModel
import androidx.navigation.findNavController
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentEditGroupnameBinding
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.MessageAPI
import com.bobi.tfca.network.response.MessageResponse
import com.bobi.tfca.util.LogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType
import okhttp3.RequestBody

class EditGroupNameFragment : BaseFragment<FragmentEditGroupnameBinding, ViewModel>(
    FragmentEditGroupnameBinding::inflate,
    null,
    true
) {

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentEditGroupnameBinding,
        viewModel: ViewModel?,
        savedInstanceState: Bundle?
    ) {
        // 初始化标题
        val groupName = publicViewModel!!.chatRoom.value!!.groupName
        binding.editGroupName.setText(groupName)
        val groupId = publicViewModel!!.chatRoom.value!!.groupId

        // 设置头部菜单的按钮
        setTopPanelStyle(binding, groupId)

    }

    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(
        binding: FragmentEditGroupnameBinding,
        groupId: Int
    ) {
        val topPanel = binding.topPanel
        val leftLayout = topPanel.leftLayout
        leftLayout.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        var rightButton = topPanel.rightButton
        rightButton.text = "确定"
        rightButton.setOnClickListener {
            // 更改群聊名称，返回详情
            val groupName = binding.editGroupName.text.toString()
            editGroupName(groupId, groupName)
        }
        val title = topPanel.titleText
        // 群聊名称
        title.text = "编辑群名"
    }

    /**
     * 更改群聊名称
     */
    private fun editGroupName(groupId: Int, groupName: String) {
        val requestBody = RequestBody.create(MediaType.parse("text/plain"), groupName)
        publicViewModel?.apply {
            request(MessageAPI::class.java).editGroupName(groupId, requestBody).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                // 更改viewmodel的值
                                val chatRoom = publicViewModel!!.chatRoom.value
                                chatRoom!!.groupName = groupName
                                publicViewModel!!.chatRoom.value = chatRoom

                                // 返回
                                requireActivity().findNavController(R.id.app_nav).navigateUp()
                            }
                        }
                    }
                }
            }
        }

    }


}